package com.jubao.ejb.dao.tests;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import junit.framework.JUnit4TestAdapter;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.junit.Test;

public class SystemDataImporter extends EJBSuperTest {
	static Logger logger = Logger.getLogger(SystemDataImporter.class);

	private String fileHome = "D:/conanwork/fatcat-workspace/com.jubao.ejb/data/";

	private String[] brandFileNames = {//--------------------------------------------------- 
	"sample/brand.xml" };

	private String[] categoryFileNames = {//--------------------------------------------------- 
	"sample/category_product.xml",// -----------------------------------------
			"sample/category_area.xml",// -----------------------------------------
			"sample/category_business.xml",// -----------------------------------------
			"sample/category_website.xml"// -----------------------------------------
	};

	private String[] brand2CategoryFileNames = {//--------------------------------------------------- 
	"sample/brand2category.xml" };

	private String[] productFileNames = { // ------------------------
	"sample/product.xml"// -----------------------------------------
	};

	private String[] product2CategoryFileNames = { // ---------------------------------
	"sample/product2category.xml" };

	private String[] product2BrandFileNames = { // ---------------------------------
	"sample/product2brand.xml" };

	private String[] storeFileNames = { // ------------------------
	"sample/store.xml"// -----------------------------------------
	};

	private String[] store2CategoryFileNames = {// --------------------------------------
	"sample/store2category.xml" // -------------------------------------------
	};

	private String[] store2ProductPriceFileNames = {// -------------------------------------------
	"sample/store2product.xml" // ------------------------------------------
	};

	private String[] storeCouponFileNames = {// -------------------------------------------
	"sample/storecoupon.xml" // ------------------------------------------
	};

	private String[] storeCouponItemFileNames = {// -------------------------------------------
	"sample/storecouponitem.xml" // ------------------------------------------
	};

	private String[] storeTransactionFileNames = {// -------------------------------------------
	"sample/storetransaction.xml" // ------------------------------------------
	};

	private String[] websiteFileNames = { // ------------------------
	"sample/website.xml"// -----------------------------------------
	};

	private String[] website2CategoryFileNames = {// --------------------------------------
	"sample/website2category.xml" // -------------------------------------------
	};

	private String[] userFileNames = {// ------------------------------------------------------
	"sample/user.xml" };

	private String[] user2CategoryFileNames = {// --------------------------------------
	"sample/user2category.xml" // -------------------------------------------
	};

	private String[] user2StoreFileNames = { "sample/user2store.xml" };

	private String[] user2ProductFileNames = { "sample/user2product.xml" };

	public SystemDataImporter() {
		super();
	}

	public static void main(String... args) {
		junit.textui.TestRunner.run(suite());
	}

	public static junit.framework.Test suite() {
		return new JUnit4TestAdapter(SystemDataImporter.class);
	}

	@Test
	public void testImport() {
		//		importBrand();
		//		importCategories();
		//		importBrand2Category();
		//		importUsers();
		//		importProducts();
		//		importProduct2Category();
		//		importProduct2Brand();
		//		importStores();
		//				importStore2Categorys();
		//		importStore2ProductPrice();
		importStoreCoupons();
		importStoreCouponItems();
		importStoreTransactions();
		//		importWebsites();
		//		importWebsite2Categorys();
		//		importUser2Categorys();
		//		importUser2Product();
		//		importUser2Store();
	}

	private void importBrand() {
		for (int i = 0; i < brandFileNames.length; i++) {
			importDataFromFile(fileHome + brandFileNames[i]);
		}
	}

	private void importCategories() {
		for (int i = 0; i < categoryFileNames.length; i++) {
			importDataFromFile(fileHome + categoryFileNames[i]);
		}
	}

	private void importBrand2Category() {
		for (int i = 0; i < brand2CategoryFileNames.length; i++) {
			importDataFromFile(fileHome + brand2CategoryFileNames[i]);
		}
	}

	public void importProducts() {
		for (int i = 0; i < productFileNames.length; i++) {
			importDataFromFile(fileHome + productFileNames[i]);
		}
	}

	public void importProduct2Category() {
		for (int i = 0; i < product2CategoryFileNames.length; i++) {
			importDataFromFile(fileHome + product2CategoryFileNames[i]);
		}
	}

	public void importProduct2Brand() {
		for (int i = 0; i < product2BrandFileNames.length; i++) {
			importDataFromFile(fileHome + product2BrandFileNames[i]);
		}
	}

	public void importStores() {
		for (int i = 0; i < storeFileNames.length; i++) {
			importDataFromFile(fileHome + storeFileNames[i]);
		}
	}

	public void importStore2Categorys() {
		for (int i = 0; i < store2CategoryFileNames.length; i++) {
			importDataFromFile(fileHome + store2CategoryFileNames[i]);
		}
	}

	public void importStore2ProductPrice() {
		for (int i = 0; i < store2ProductPriceFileNames.length; i++) {
			importDataFromFile(fileHome + store2ProductPriceFileNames[i]);
		}
	}

	public void importStoreCoupons() {
		for (int i = 0; i < storeCouponFileNames.length; i++) {
			importDataFromFile(fileHome + storeCouponFileNames[i]);
		}
	}

	public void importStoreCouponItems() {
		for (int i = 0; i < storeCouponItemFileNames.length; i++) {
			importDataFromFile(fileHome + storeCouponItemFileNames[i]);
		}
	}

	public void importStoreTransactions() {
		for (int i = 0; i < storeTransactionFileNames.length; i++) {
			importDataFromFile(fileHome + storeTransactionFileNames[i]);
		}
	}

	public void importWebsites() {
		for (int i = 0; i < websiteFileNames.length; i++) {
			importDataFromFile(fileHome + websiteFileNames[i]);
		}
	}

	public void importWebsite2Categorys() {
		for (int i = 0; i < website2CategoryFileNames.length; i++) {
			importDataFromFile(fileHome + website2CategoryFileNames[i]);
		}
	}

	public void importUsers() {
		for (int i = 0; i < userFileNames.length; i++) {
			importDataFromFile(fileHome + userFileNames[i]);
		}
	}

	public void importUser2Categorys() {
		for (int i = 0; i < user2CategoryFileNames.length; i++) {
			importDataFromFile(fileHome + user2CategoryFileNames[i]);
		}
	}

	public void importUser2Store() {
		for (int i = 0; i < user2StoreFileNames.length; i++) {
			importDataFromFile(fileHome + user2StoreFileNames[i]);
		}
	}

	public void importUser2Product() {
		for (int i = 0; i < user2ProductFileNames.length; i++) {
			importDataFromFile(fileHome + user2ProductFileNames[i]);
		}
	}

	private void importDataFromFile(String fileName) {
		if (logger.isInfoEnabled()) {
			logger.info("Loading properties file from " + fileName);
		}
		logger.info("Loading properties file from " + fileName);

		// Read from an input stream
		try {
			InputStream is = new BufferedInputStream(new FileInputStream(fileName));
			logger.info("The sequence of records is very important,that means the previous one will be saved first "
					+ "and the later ones maybe depends on the previous one. "
					+ "Entity name first character must be capitalized and the fields must be lowcase");

			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(is);

			dataImportBusinessRemote.importData(document.asXML());
			//			dataImportBusinessRemote.importData(document);

			logger.info("sleep 3 seconds........");
			Thread.sleep(3000);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}

	/**
	 * Import data from text
	 * 
	 * @param text
	 * @throws IOException
	 * @throws DocumentException
	 * @throws InterruptedException
	 */
	public void importDataFromText(String text) throws IOException, DocumentException, InterruptedException {
		// If you have some XML as a String you can parse it back into a
		// Document again using the helper method DocumentHelper.parseText()

		// Document document = DocumentHelper.parseText(text);
		// treeWalk(document);
	}

}